package no.group.gowther.util;

import java.util.Arrays;

public class PageUtils {
    public static long[] calPageWindow(long windowSize, long currentPage, long totalPage) {
        long start = currentPage - windowSize / 2;
        long end = (windowSize & 1) == 1 ? currentPage + windowSize / 2 : currentPage + windowSize / 2 - 1;
        if (end > totalPage) {
            start = Math.max(1, start - (end - totalPage));
            end = totalPage;
        } else if (start < 1) {
            end = Math.min(totalPage, end + (1 - start));
            start = 1;
        }
        return new long[]{start, end};
    }

    public static boolean isLast(int pageNum, int pageSize, int index, long total) {
        return (long) (pageNum - 1) * pageSize + index + 1 == total;
    }

    public static void main(String[] args) {
        long[] ints = calPageWindow(5, 1, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 2, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 3, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 4, 10);
        System.out.println(Arrays.toString(ints));

        System.out.println("=====================");

        ints = calPageWindow(5, 7, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 8, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 9, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 10, 10);
        System.out.println(Arrays.toString(ints));

        ints = calPageWindow(5, 15, 10);
        System.out.println(Arrays.toString(ints));
    }
}
